﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Data;

namespace TheMovieDb.Example.Converters
{
    [ValueConversion(typeof(object), typeof(string))]
    public class ThumbnailConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            var images = value as List<TmdbImage>;
            if (images == null)
                return null;

            var image = images.FirstOrDefault(i => i.ImageInfo.Type == parameter.ToString() && i.ImageInfo.Size == "thumb");
            if (image != null)
                return image.ImageInfo.Url;
            return null;
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            // we don't intend this to ever be called
            return null;
        }
    }
}
